package com.syxx.oa.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.syxx.oa.domain.EmpLevel;
import com.test.jbpm.Pager;
import com.test.jbpm.dao.impl.BaseDao;

@Repository  //代表存储层bean
public class EmpLevelDao  extends BaseDao {
	public void addOrUpdate(EmpLevel bean) {
		getHibernateTemplate().saveOrUpdate(bean);
	}

	public void delById(Long id) {
		getHibernateTemplate().delete(
			getHibernateTemplate().load(EmpLevel.class, id)	
		);
	}

	public EmpLevel findById(Long id) {
		return (EmpLevel)getHibernateTemplate().load(EmpLevel.class, id);
	}

	@SuppressWarnings("unchecked")
	public Pager<EmpLevel> findList() {
		return findData("from EmpLevel");
	}
	
	public List<EmpLevel> findListBy(final Map<String,Object> key){
		@SuppressWarnings("unchecked")
		List<EmpLevel> list = getHibernateTemplate().executeFind(new HibernateCallback() {
			
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql = "from EmpLevel d";
				if(key != null && !key.isEmpty()){
					hql += " where ";
					int i=0;
					for (String k : key.keySet()) {
						if(i==key.keySet().size()-1){
							hql+=" (d."+k+"=?) ";
						}else{
							hql+=" (d."+k+"=?) and ";
						}
						i++;
					}
				} 
				
				Query query = session.createQuery(hql);
				if(key != null && !key.isEmpty()){
					int i=0;
					for (Object value : key.values()) {
						query.setParameter(i, value);
						i++;
					}
				}
				return query.list();
			}
		});
		return list;
	}
}
